<?php
class core{
	/*
	 * Current version information
	 */
	public $version = "1.0.0dev";
	/*
	 * Player Information
	 */
	public $player = 0;
	/*
	 * Enemy Information
	 * This is only used in 'fight.php' currently.
	 */
	public $enemy = 0;
	/*
	 * Session Information
	 */
	public $session = array();
	/*
	 * Input Variables (GET, and POST)
	 */
	public $input = array();
	/*
	 * Game Settings
	 */
	public $settings = array();

	/*
	 * Run some basic functions when this class is called.
	 */
	public function __construct(){
		global $settings;
		$this->settings = $settings;
		//parse cookies
		foreach($_COOKIE as $key=>$value){
			$this->session[$key] = $value;
		}
		//parse sessions
		foreach($_SESSION as $key=>$value){
			if(!isset($this->session[$key])){
				$this->session[$key] = $value;
			}
		}
		//parse GET
		foreach($_GET as $key=>$value){
			$this->input[$key] = $value;
		}
		//parse POST
		foreach($_POST as $key=>$value){
			if(!isset($this->input[$key])){
				$this->input[$key] = $value;
			}
		}
		$this->checkLogin();
	}

	public function sanitize($string, $type=HTML){
		switch($type){
			default:
				$string = htmlentities($string, ENT_NOQUOTES);
				break;
		}
		return $string;
	}

	/*
	 * Create a new session
	 */
	public function addSession($name, $value, $type=1){
		if($type == 1){
			$_SESSION[$name] = $value;
		}else{
			setcookie($name, $value, time() + 604800, "/phprpg/", "localhost");
		}
	}

	/*
	 * Remove Session
	 */
	public function remSession($name){
		if(isset($_SESSION[$name])){
			$_SESSION[$name] = "";
		}
		if(isset($_COOKIE[$name])){
			setcookie($name, "", time() - 1, "/phprpg/", "localhost");
		}
	}
	/*
	 * Check player login
	 * If logged in, set $this->player
	 * If not (and page requires login)
	 * redirect to login page
	 */
	public function checkLogin(){
		global $db,$cfg;
		if(!defined("NO_LOGIN")){
			if(isset($this->session['uid']) && isset($this->session['hash'])){
				$getUser = $db->select("players","*","pid=".intval($this->session['uid']));
				if($db->recordcount($getUser) == 1){
					$user = $db->fetchrow($getUser);
					$check = md5(md5($user['pid']).md5($this->getIP()).md5($cfg['login']['hash']));
					if($check == $this->session['hash']){
						$getGroup = $db->select("playergroups","*","id=".$user['playergroup']);
						$user['pgroup'] = $db->fetchrow($getGroup);
						$this->player = $user;
						$loggedIn = 1;
					}
				}
			}
			if($loggedIn != 1){
				header("location: {$this->settings['gameurl']}login.php");
			}
		}
	}

	/*
	 * Get Connection IP
	 */
	public function getIP(){
		return $_SERVER['REMOTE_ADDR'];
	}

	/*
	 * Output error message
	 */
	public function inline_error($errors=array()){
		$return = '<div class="error">
<ul>';
		foreach($errors as $error){
			$return .= "\n	<li>".$error."</li>";
		}
		$return .= "
</ul>
</div>";
		return $return;
	}

	/*
	 * Output an error page (non-technical)
	 */
	public function error_page($message){
		global $headerincludes, $header, $footer, $templates;
		eval("\$error_page = \"".$templates->get("error_page")."\";");
		$templates->output($error_page);
	}
}
?>